package amartin47;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import say.swing.JFontChooser;

public class LyricsDrawStringWindow  extends JFrame implements LyricsWindow {
	LyricsClient client;
	RenderText textRenderer;
	TextLayoutCenter centerText;
	
	public LyricsDrawStringWindow() {
	    super ("Lyrics Display");    
	    
	    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 		this.setMinimumSize(new Dimension(900,900));
 		this.setResizable(false);
 		this.getContentPane().setBackground(Color.BLACK);

 		textRenderer = new RenderText();
 		centerText = new TextLayoutCenter();
 		this.add(centerText);
 		
 		
	    pack();
	    setVisible (true);
		
 		this.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				if(arg0.getKeyChar() == 'f')
				{
					   JFontChooser fontChooser = new JFontChooser();
					   int result = fontChooser.showDialog(null);
					   if (result == JFontChooser.OK_OPTION)
					   {
					        Font font = fontChooser.getSelectedFont(); 
					        centerText.setFont(font);
					        updateSlide("Font Name: " + font.getName() + " <BR> " +
					        			"Font Size: " + font.getSize() + " <BR> " +
					        			"The quick brown fox jumps over the lazy dog");
					   }
				}
				
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
 			
 		});
	    
		this.addWindowListener(new WindowAdapter() {
		      public void windowClosing(WindowEvent e) {
		    	  if(client != null)
		    	  {
		    		  client.stopClient = true;
		    	  }
		      }
		});
		
		client = new LyricsClient(this);
	}
	
	public void updateSlide(String lyrics)
	{
		//textRenderer.updateText(lyrics);
		centerText.updateString(lyrics.replaceAll("<[AC-Zac-z/][A-Za-z0-9]*>", ""));
	}
	
	private class RenderText extends JPanel
	{
		public RenderText() {
			this.setBackground(Color.BLACK);
			text = new String[0];
		}
		
		String[] text;
		public void updateText(String text)
		{
			this.text = text.split("<br>");
			for(int i = 0; i < this.text.length; i++)
			{
				this.text[i] = this.text[i].replaceAll("<[AC-Zac-z/][A-Za-z0-9]*>", "");
			}
			
			this.repaint();
		}
		protected void paintComponent(Graphics g)
		{
			super.paintComponent(g);
			g.setColor(Color.WHITE);
			int y = 50;
			for(String s : text)
			{
				g.drawString(s, 50, y);
				y += 30;
			}
		}
	}
	
	public static final void main(final String[] args) throws Exception {
		
 		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 
 		final JFrame fullscreenFrame = new LyricsDrawStringWindow();
 		//fullscreenFrame.setUndecorated(true);
 		fullscreenFrame.setVisible(true);
 		//GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(fullscreenFrame);
 	}
}
